package com.sservice.web.controller.admin;

import com.sservice.common.Constants;
import com.sservice.model.dao.IStoreDao;
import com.sservice.model.dao.PurchaseOrderBillMapper;
import com.sservice.model.vo.StoreVo;
import com.sservice.service.service.ILadingBillService;
import com.sservice.service.service.IOfferService;
import com.sservice.service.service.IStoreAccountService;
import com.sservice.service.service.IStoreService;
import com.sservice.urp.entity.User;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.model.entity.*;
import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin/store")
public class StoreController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IStoreService depService;
    @Autowired
    private IStoreDao storeDao;
    @Autowired
    private IStoreAccountService saService;
    @Autowired
    private ILadingBillService ldService;
    @Autowired
    private PurchaseOrderBillMapper pobService;
    @Autowired
    private IOfferService offerService;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Integer> create(Store arg) {
        int i = storeDao.countByName(arg);
        if (i > 0) {
            return new BaseResult(false, "仓库重名", 403);
        }
        int service_res = depService.insertSelective(arg);
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public BaseResult detail(Store arg) {
        Store service_res = depService.selectByPrimaryKey(arg.getId());
        return new BaseResult(true, service_res);
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult<Object> delete(Store arg) {
        arg.setStatus("0"); //逻辑删除
        LadingBill lb = new LadingBill();
        lb.setStoreId(arg.getId().toString());
        int num = ldService.selectByStoreId(lb);
        if (num > 0) {
            return new BaseResult(false, "仓库关联提单数据，无法删除", 403);
        }
        PurchaseOrderBill pob = new PurchaseOrderBill();
        pob.setStoreId(String.valueOf(arg.getId()));
        if (pobService.selectByStoreId(pob) > 0) {
            return new BaseResult(false, "仓库关联收货单数据，无法删除", 403);
        }
        Offer offer = new Offer();
        offer.setDeliverPlaceIds(arg.getId().toString());
        int total = pobService.queryByStoreId(String.valueOf(arg.getId()));
        if (total > 0) {
            return new BaseResult(false, "仓库关联收货单数据，无法删除", 403);
        }
        int service_res = depService.updateByPrimaryKeySelective(arg);
        return new BaseResult<Object>(true, service_res);
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Integer> update(Store arg) {
        int service_res = depService.updateByPrimaryKeySelective(arg);
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/plus", method = RequestMethod.POST)
    public BaseResult<Object> plus(Store arg, Double number, HttpSession session) {
        StoreAccount sa = new StoreAccount();
        sa.setStoreId(String.valueOf(arg.getId()));
        User user = (User) session.getAttribute(Constants.USER);
        if (user == null) {
            return new BaseResult<Object>(false, "未登录!", 401);
        }
        sa.setNumberCanSell(number);

        Object sa_res = saService.plusNumber(sa, String.valueOf(user.getId()));
        BaseResult<Object> res = new BaseResult<Object>(true, sa_res);
        return res;
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(StoreVo arg) {
        List<Store> list = depService.queryByPage(arg);
        int total = depService.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        System.out.println("Size:" + arg.getSize());
        System.out.println("getSortField:" + arg.getSortField());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/queryPinyin", method = RequestMethod.GET)
    public ListResult queryPinyin(StoreVo arg) throws PinyinException {
        List<Store> list = depService.queryByPage(arg);
        List<StoreVo> vo = new ArrayList<StoreVo>();
        for (Store ele :
                list) {
            StoreVo cv = new StoreVo();
            cv.setName(ele.getName());
            cv.setId(ele.getId());
            cv.setPinyin(PinyinHelper.convertToPinyinString(cv.getName(), "", PinyinFormat.WITHOUT_TONE));
            vo.add(cv);
        }
        int total = depService.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult res = new ListResult(true, vo);
        res.setPage(page);
        return res;
    }
}
